package org.ourgrid.worker.collectors.gmond;

import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.ourgrid.common.util.OS;
import org.ourgrid.worker.collectors.common.Metric;
import org.w3c.dom.NodeList;

public abstract class GMondUtils {

	private static GMondUtils uniqueInstance;

	public abstract boolean isGMondInstalled();

	protected abstract void startGMondDaemon() throws IOException;

	public abstract void stopGMond() throws IOException;

	public abstract boolean isGMondRunning() throws IOException;

	public abstract int getGmondPort(String ConfigFile) throws GMondCollectorException;

	public abstract String getGmondAddress() throws GMondCollectorException;
	
	public abstract String getGmondConfigFile(); 

	public void startGMond() throws IOException {
		if( isGMondInstalled() ) {			
			if( ! isGMondRunning() ) {
				startGMondDaemon();
			}
		}
	}

	public static List<Metric> generateMetricList(NodeList nodeList) {
		List<Metric> metrics = Collections.synchronizedList(new LinkedList<Metric>());

		for (int k = 0; k < nodeList.getLength(); k++) {
			metrics.add(GMondMetric.create(nodeList.item(k)));
		}

		return metrics;
	}

	public static GMondUtils getInstance() {
		if(uniqueInstance == null){
			if(OS.isFamilyWindows()){
				uniqueInstance = new GMondUtilsWindows();
			}else if(OS.isFamilyUnix()){
				uniqueInstance = new GMondUtilsLinux();
			}
		}
		return uniqueInstance;
	}
}
